<?php

error_reporting(E_ALL);

header('Content-Type: text/html; charset=UTF-8');
// Объявляем константу APPLICATION_PATH - это путь к папке application
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Объявляем константу APPLICATION_ENV - prodution или development
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

// добавляем папку /library к include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Создаем приложение, инициализуруем и запускаем его
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
  
 $config = new Zend_Config_Ini('../application/configs/application.ini', 'development');
 Zend_Registry::set('config', $config);    
// соединение с БД
    $params = array(
        'host' => $config->resources->db->params->host,
        'username' => $config->resources->db->params->username,
        'password' => $config->resources->db->params->password,
        'dbname' => $config->resources->db->params->dbname,
    );
    
    $db = Zend_Db::factory('pdo_mysql', $params);
    Zend_Registry::set('db', $db);
    $db->query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
    
$application->bootstrap();
// получаем роутер
$router = Zend_Controller_Front::getInstance()->getRouter();
// добавляем маршрут для действия /user/login
$router->addRoute('user-login',
        new Zend_Controller_Router_Route('/login/',array('controller' => 'user', 'action' => 'login'))
);

$router->addRoute('user-logout',
        new Zend_Controller_Router_Route('/logout/',  array('controller' => 'user', 'action' => 'logout'))
);
//News Router
$router->addRoute('news',
     new Zend_Controller_Router_Route('/news/', array('module' => 'news', 'controller' => 'index', 'action' => 'index'))
);

$router->addRoute('viewnews',
     new Zend_Controller_Router_Route('/news/:newsAlias', array('module' => 'news', 'controller' => 'index', 'action' => 'view'))
);

$router->addRoute('adminviewnews',
     new Zend_Controller_Router_Route('/admin/news/:newsAlias', array('module' => 'admin', 'controller' => 'news', 'action' => 'view'))
);

$router->addRoute('admineditnews',
     new Zend_Controller_Router_Route('/admin/news/edit/:newsId', array('module' => 'admin', 'controller' => 'news', 'action' => 'edit'))
);

$router->addRoute('admindeletenews',
     new Zend_Controller_Router_Route('/admin/news/delete/:newsId', array('module' => 'admin', 'controller' => 'news', 'action' => 'delete'))
);

$router->addRoute('adminaddnews',
     new Zend_Controller_Router_Route('/admin/news/add/', array('module' => 'admin', 'controller' => 'news', 'action' => 'add'))
);
//
//Articles Router
$router->addRoute('articles',
     new Zend_Controller_Router_Route_Static('/articles/', array('module' => 'articles', 'controller' => 'index', 'action' => 'index'))
);

$router->addRoute('viewarticles',
     new Zend_Controller_Router_Route('/articles/:articlesAlias', array('module' => 'articles', 'controller' => 'index', 'action' => 'view'))
);

$router->addRoute('adminviewarticles',
     new Zend_Controller_Router_Route('/admin/articles/:articlesAlias', array('module' => 'admin', 'controller' => 'articles', 'action' => 'view'))
);

$router->addRoute('admineditarticles',
     new Zend_Controller_Router_Route('/admin/articles/edit/:articlesId', array('module' => 'admin', 'controller' => 'articles', 'action' => 'edit'))
);

$router->addRoute('admindeletearticles',
     new Zend_Controller_Router_Route('/admin/articles/delete/:articlesId', array('module' => 'admin', 'controller' => 'articles', 'action' => 'delete'))
);

$router->addRoute('adminaddarticles',
     new Zend_Controller_Router_Route('/admin/articles/add/', array('module' => 'admin', 'controller' => 'articles', 'action' => 'add'))
);
//
//Pages Router
$router->addRoute('pages',
     new Zend_Controller_Router_Route('/pages/', array('module' => 'pages', 'controller' => 'index', 'action' => 'index'))
);

$router->addRoute('viewpages',
     new Zend_Controller_Router_Route('/:pagesAlias/', array('module' => 'pages', 'controller' => 'index', 'action' => 'view'),
              array('pagesAlias' => '(?!admin$|news$|articles$|user$|login$|logout$|support$).+')
             )
);

$router->addRoute('adminviewpages',
     new Zend_Controller_Router_Route('/admin/pages/:pagesAlias', array('module' => 'admin', 'controller' => 'pages', 'action' => 'view'))
);

$router->addRoute('admineditpages',
     new Zend_Controller_Router_Route('/admin/pages/edit/:pagesId', array('module' => 'admin', 'controller' => 'pages', 'action' => 'edit'))
);

$router->addRoute('admindeletepages',
     new Zend_Controller_Router_Route('/admin/pages/delete/:pagesId', array('module' => 'admin', 'controller' => 'pages', 'action' => 'delete'))
);

$router->addRoute('adminaddpages',
     new Zend_Controller_Router_Route('/admin/pages/add/', array('module' => 'admin', 'controller' => 'pages', 'action' => 'add'))
);
//
//Users Router

$router->addRoute('adminviewusers',
     new Zend_Controller_Router_Route('/admin/users/:usersId', array('module' => 'admin', 'controller' => 'users', 'action' => 'view'))
);

$router->addRoute('admineditusers',
     new Zend_Controller_Router_Route('/admin/users/edit/:usersId', array('module' => 'admin', 'controller' => 'users', 'action' => 'edit'))
);

$router->addRoute('admindeleteusers',
     new Zend_Controller_Router_Route('/admin/users/delete/:usersId', array('module' => 'admin', 'controller' => 'users', 'action' => 'delete'))
);

$router->addRoute('adminaddusers',
     new Zend_Controller_Router_Route('/admin/users/add/', array('module' => 'admin', 'controller' => 'users', 'action' => 'add'))
);
//
$application->run();